struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
    if (dummy == NULL) {
        return head;
    }
    dummy->next = head;
    struct ListNode* p = dummy;

    while (p->next != NULL) {
        if (p->next->val == val) {
            struct ListNode* temp = p->next;
            p->next = temp->next;
            free(temp);
        }
        else {
            p = p->next;
        }
    }

    struct ListNode* newHead = dummy->next;
    free(dummy);
    return newHead;
}